package cn.wit.summit.chwan.tools.webview;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import cn.wit.summit.chwan.utils.NetWorkUtils;

public class WebViewClientYQW extends WebViewClient {
    Context context;
    WebViewClientInterface clientInterface;
    private boolean isError=false;

    public interface WebViewClientInterface{
    public void setUrl(String url);
    public void showLoding();
    public void showLodingFailed();
}
    public WebViewClientYQW(Context context,WebViewClientInterface clientInterface) {
        this.context = context;
        this.clientInterface=clientInterface;
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url2) {

        if (NetWorkUtils.isNetworkConnected(context)) {
            if (!url2.startsWith("weixin:")) {
                if (url2.startsWith("http") || url2.startsWith("https") || url2.startsWith("file://")) {
//                    url = url2;
                    clientInterface.setUrl(url2);
                } else {
                    try {
                       context. startActivity(new Intent("android.intent.action.VIEW", Uri.parse(url2)));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return true;
                }
                return super.shouldOverrideUrlLoading(view, url2);

            } else {
                try {
                    context.  startActivity(new Intent("android.intent.action.VIEW", Uri.parse(url2)));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } else {
        }
        return true;
    }

    @Override
    public void onPageStarted(WebView view, String url2, Bitmap favicon) {
        super.onPageStarted(view, url2, favicon);

        if (url2.startsWith("http") || url2.startsWith("https") || url2.startsWith("file://")) {
//                view.loadUrl(url);// 当打开新链接时，使用当前的 WebView，不会使用系统其他浏览器
            clientInterface.setUrl(url2);
        }
        clientInterface.showLoding();
//        showLoding();
    }

    @Override
    public void onPageFinished(WebView view, String url2) {
        super.onPageFinished(view, url2);
        if (url2.startsWith("http") || url2.startsWith("https") || url2.startsWith("file://")) {
//                view.loadUrl(url);// 当打开新链接时，使用当前的 WebView，不会使用系统其他浏览器
//            url = url2;//记录url
            clientInterface.setUrl(url2);
        }
        //显示默认的失败界面的url
        if (url2.equals("data:text/html,chromewebdata") || isError) {
//            showLodingFailed();
            clientInterface.showLodingFailed();
            return;
        }
        view.getSettings().setBlockNetworkImage(false);
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        super.onReceivedError(view, errorCode, description, failingUrl);
        isError = true;
        view.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
//        showLodingFailed();
        clientInterface.showLodingFailed();
    }
}